<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Permissionmodel extends CI_Model {

	protected $_CI;

	public static $searchField = array();
	public static $likeField = array();
	public static $sortField = array();

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  Group Basic Functions
| -------------------------------------------------------------------
*/
	public function getPermissionList(){
		$this->db->from('permission');
		$this->db->order_by('permission_category','asc');
		$this->db->order_by('permission_name','asc');
		$query = $this->db->get();
		$list = $query->result_array();
		
		return $list;
	}

	public function createPermission($info){
		$this->db->insert('permission',$info);
		return true;
	}

	public function editPermission($permission_id,$info){
		$this->db->where('permission_id',$permission_id);
		$this->db->update('permission',$info);
	}

	public function getPermissionAction($permission_ids){
		if(!is_array($permission_ids)) $permission_ids = array($permission_ids);
		if(empty($permission_ids)) $permission_ids[] = -1;

		$this->db->select('permission_action');
		$this->db->from('permission');
		$this->db->where_in('permission_id', $permission_ids);
		$query = $this->db->get();
		$action = $query->result_array();

		$permission = array();
		foreach($action as $record){
			if(validate($record['permission_action'])){
				$permission = array_merge($permission,json_decode($record['permission_action']));
			}
		}

		$permission = array_unique($permission);

		return $permission;
	}

	public function getPermissionCount(){
		$total = $this->db->count_all('permission');
		return $total;
	}
}